package com.ews.onlineexchange.controller;

import java.io.Serializable;

import javax.enterprise.context.Conversation;
import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import com.ews.onlineexchange.service.UserService;

@Model
public class UserController implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	@Inject
	UserService userService;

	private String		oldPassword;
	private String		newPassword;
	private String		confirmPassword;
    
    @Inject
    protected Conversation conversation;
	
	public void initConversation() {    
	    if (conversation.isTransient()) {
	        conversation.begin();
	    }
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	
//	@Pattern(regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&_])[A-Za-z\\d$@$!%*?&_]{8,}", message = "密码必须以字母开头，长度至少8位，需包含大小写字母、数字和特殊字符")
//	@NotNull
	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}
//	@Inject
//	EntityManager entityManager;
//	@Transactional
//	public String changePassword() {
//		try {
//			User user = userService.findLoginedUser();
//			user.setAliasp(AesUtil.AESEncode(user.getUsername(),getNewPassword()));
//			entityManager.merge(user);
//			keycloakUserService.changePassword(user, getOldPassword(), getNewPassword());
//		} catch (InvalidUsernameAndPasswordException e) {
//			FacesContext.getCurrentInstance().addMessage("oldPassword", new FacesMessage(FacesMessage.SEVERITY_ERROR, "原密码错误，请输入正确密码后重试。", ""));
//			return null;
//		}
//		FacesContext.getCurrentInstance().addMessage("", new FacesMessage("密码修改成功。"));
//		return "ok";
//	}
//
//	@Inject
//	UserHome userHome;
//	@Transactional
//	public String resetPassword() {
//		User user = userHome.getInstance();
//		if(user == null) {
//			FacesContext.getCurrentInstance().addMessage("newPassword", new FacesMessage(FacesMessage.SEVERITY_ERROR, "未找到此用户！。", ""));
//			return null;
//		}
//		try {
//			user.setAliasp(AesUtil.AESEncode(user.getUsername(),getNewPassword()));
//			entityManager.merge(user);
//			keycloakUserService.resetPassword(user, getNewPassword());
//		} catch (InvalidUsernameAndPasswordException e) {
//			FacesContext.getCurrentInstance().addMessage("newPassword", new FacesMessage(FacesMessage.SEVERITY_ERROR, "重置密码时出现错误，请稍后重试！", ""));
//			return null;
//		}
//		FacesContext.getCurrentInstance().addMessage("", new FacesMessage("密码重置成功。"));
//		return "ok";
//	}
	
}
